import service from "@/common/service";
import {getRdsUrl, rdsRequest} from '@/hooks/rdsServices';

const uniqueKey = '商品退货申请单';
const billType = '511';
const billKind = '51';
/**
 * 单据审核
 * @param {{BillId: '单据号', UserNo: '用户ID'}} objectData 单据号数组
 * @param {() => void} cb 回调方法
 */
const billAudit = (objectData, cb) => {
    const rdsUrl = getRdsUrl();

    const that = this;
    const { BillId } = objectData;
    uni.showModal({
        title: '提示',
        content: `请确认，审核单据[${BillId}]吗?`,
        success: async function (res) {
            if (res.confirm) {
                service.enjoyShowLoading('正在加载...');
                const result = await rdsRequest(rdsUrl.scApi, uniqueKey, 'EnjoyBillAudit', objectData, billType);
                service.enjoyHideLoading();
                uni.showToast({
                    duration: 3500,
                    icon: 'none',
                    title: `审核成功！`,
                });
                cb && cb();
            } else if (res.cancel) {
            }
        },
    });
}

/**
 * 单据删除
 * @param {[{BillId: '单据号'}]} objectData 单据号数组
 * @param {() => void} cb 回调方法
 */
const billsDelete = (objectData, cb) => {
    const rdsUrl = getRdsUrl();

    const that = this;
    uni.showModal({
        title: '提示',
        content: `请确认，删除单据[${objectData.reduce((billId, item)=> item.BillId + (billId ? `, ${billId}` : ''), '')}]吗?`,
        success: async function (res) {
            if (res.confirm) {
                service.enjoyShowLoading('正在加载...');
                const result = await rdsRequest(rdsUrl.scApi, uniqueKey, 'EnjoyBillsDelete', objectData, billType);
                service.enjoyHideLoading();
                uni.showToast({
                    duration: 3500,
                    icon: 'none',
                    title: `删除成功！`,
                });
                cb && cb();
            } else if (res.cancel) {
            }
        },
    });
}

/**
 * 单据保存
 * @param {object} objectData 单据数据
 * @param {() => void} cb 回调方法
 */
const billSaveNew = async (objectData, cb) => {
    const rdsUrl = getRdsUrl();

    service.enjoyShowLoading('正在加载...');
    const result = await rdsRequest(rdsUrl.scApi, '商品退货申请单', 'EnjoyBillSaveNew', objectData, '511');
    uni.showToast({
        duration: 3500,
        icon: 'none',
        title: `保存成功！`
    });
    cb && cb();
    service.enjoyHideLoading();
}

export  {
    billKind,
    billType,
    uniqueKey,
    billAudit,
    billsDelete,
    billSaveNew,
}
